//
//  PVDsListCellOperButton.swift
//  GreenDeal
//
//  Created by Moennig Q on 13/12/2022.
//

import UIKit

enum PVDsListCellOperType: Int {
    case barcode = 0, checklist, signature, cesfile
}
class PVDsListCellOperButton: UIControl{
    var typeImageView: UIImageView!
    var finishImageView: UIImageView!
    var contentLabel: BaseLabel!
    //IB init
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }
    //纯代码创建
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpView()
    }
    //MARK: - 没完成的试图
    private func setUpView() {
        self.layer.cornerRadius = Constant.StandardCornerRadius
        self.clipsToBounds = true
        //typeImageView
        typeImageView = UIImageView()
        self.addSubview(typeImageView)
        typeImageView.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.width.equalTo(typeImageView.snp.height).multipliedBy(1.3)
            make.bottom.equalToSuperview()
        }
        //finishImageView
        finishImageView = UIImageView()
        finishImageView.contentMode = .scaleAspectFit
        self.addSubview(finishImageView)
        finishImageView.snp.makeConstraints { make in
            make.left.equalTo(typeImageView.snp.right)
            make.right.top.bottom.equalToSuperview()
            make.width.equalTo(finishImageView.snp.height)
        }
        //contentLabel
        contentLabel = BaseLabel()
        contentLabel.setFont(R.font.interMedium(size: 13))
        contentLabel.textAlignment = .center
        self.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(typeImageView.snp.right)
            make.right.top.bottom.equalToSuperview()
        }
    }
    func setData(_ type: PVDsListCellOperType, finishCnt: Int, plantCnt: Int){
        var image: UIImage?
        switch type {
        case .barcode:
            image = R.image.installation_list_cell_barcode()
        case .checklist:
            image = R.image.installation_list_cell_checklist()
        case .signature:
            image = R.image.installation_list_cell_signature()
        case .cesfile:
            image = R.image.installation_list_cell_ces()
        
        }
        self.typeImageView.image = image
        if plantCnt > 0 && finishCnt >= plantCnt {//完成
            self.backgroundColor = UIColor(hexStr: "#3796571A")
            self.contentLabel.text = ""
            self.finishImageView.image = R.image.installation_list_cell_finish()
        }else{//未完成
            self.backgroundColor = UIColor(hexStr: "#F5F5F5")
            self.contentLabel.attributedText =  self.getAttributeString(finishCnt: "\(finishCnt)", plantCnt: "\(plantCnt)")
            self.finishImageView.image = nil
        }
    }
    private func getAttributeString(finishCnt: String, plantCnt: String)->NSMutableAttributedString{
        let subStr = plantCnt
        let content = finishCnt + "/" + subStr
        let localtion = (content as NSString).range(of: subStr)
        
        let attrString = NSMutableAttributedString(string: content)

        let attr: [NSAttributedString.Key : Any] = [.foregroundColor: UIColor.black]

        let attr2: [NSAttributedString.Key : Any] = [.foregroundColor: UIColor(hexStr: "#00000080")]

        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        attrString.addAttributes(attr2, range: NSRange(location: localtion.location, length: localtion.length))
        return attrString
    }
}
